{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Untitled2.ipynb",
      "provenance": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/letianzj/QuantResearch/blob/master/notebooks/fama_french.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "o7fgRJUECKFW",
        "colab_type": "text"
      },
      "source": [
        "### Introduction\n",
        "\n",
        "Factor models such as the APT claim that an asset's expected return comes from its exposures to factor risk premiums. In equilibrium, only factor risks are compensated because idiosyncratic risk can be diversified away. If the factors are complete, there will be no market anomaly or a manager's performance attributes completely to factor exposures; otherwise, the manager is expected to find a new factor and earn extra returns. Therefore it becomes an escape-and-catch game where practitioners are struggling to find new factors and enjoy excess returns, and then others catching up and alpha wears away.\n",
        "\n",
        "A good factor model yields smaller market anomaly or alpha. In terms of variance, the factor covariance matrix would have a higher explanation power on assets' risks. Factor models are cross-sectional in that they try to explain expected returns across assets. Yet the covariance matrix is estimated across time. Practitioners also try to timing the market factors to take advantage of working factors and avoid factors with little effects in the next period. In sum, a manager value lies on discovering new factors as well as timing or allocating on existing factors.\n",
        "\n",
        "CAPM, as a special case, has only one factor, the market risk premium. The expected return for any asset, efficient or non efficient, should lie on the SML line.  This is in contrast to CML line, that defines efficient portfolios on the tangent line. The efficient portfolio is perfectly correlated with market portfolio $\\rho=1$ so that\n",
        "\n",
        "$$\n",
        "\\beta = \\frac{cov(r, r_M)}{var(r_M)}=\\rho \\times \\frac{\\sigma_r}{\\sigma_{r_M}}=\\frac{\\sigma_r}{\\sigma_{r_M}}\n",
        "$$\n",
        "\n",
        "and hence performance assessment using Sharpe ratio.\n",
        "\n",
        "Factor model theory is hard to verify. Each stock on each time is subject to sample bias, and stocks' error terms are correlated (if AAPL outperforms, FB tends to outperform). One approach taken by [Fama-MacBeth](https://en.wikipedia.org/wiki/Fama%E2%80%93MacBeth_regression), is to use two-step regressions (time-series and then cross-sectional) on panel data to alleviate cross-sectional correlations.\n",
        "\n",
        "In [Fama-French three-factor model](https://en.wikipedia.org/wiki/Fama%E2%80%93French_three-factor_model), the authors verify their assumptions on $25$ portfolios. Specifically, \n",
        "1. To construct factors, first divide stocks into Big (B) and Small (S) according to market cap; and divide into Low (L), Middle (M), High (H) according to B/P ratio. This leads to six groups or portfolios: B/L, B/M, B/H, S/L, S/M, S/H.\n",
        "2. Calculate historical value-weighted returns of these six portfolios.\n",
        "3. Define factor returns as long-short:\n",
        "$$\n",
        "\\begin{aligned}\n",
        "SMB&=\\frac{S/L+S/M+S/H}{3}-\\frac{B/L+B/M+B/H}{3} \\\\\\\\\n",
        "HML&=\\frac{S/H+B/H}{2}-\\frac{S/L+B/L}{2}\n",
        "\\end{aligned}\n",
        "$$\n",
        "\n",
        "The $25$ portfolios to be explained are combinations of $5$ market cap groups and $5$ B/P groups.\n",
        "\n",
        "The factors are kindly published [here](https://mba.tuck.dartmouth.edu/pages/faculty/ken.french/data_library.html). Below we examine the betas of SPDR sector ETFs."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "xm-eJ0J1CPDB",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 357
        },
        "outputId": "e3e4db41-e98e-422f-ce13-cfd2fa2cb5d4"
      },
      "source": [
        "# !pip install yfinance"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Collecting yfinance\n",
            "  Downloading https://files.pythonhosted.org/packages/c2/31/8b374a12b90def92a4e27d0fc595fc43635f395984e36a075244d98bd265/yfinance-0.1.54.tar.gz\n",
            "Requirement already satisfied: pandas>=0.24 in /usr/local/lib/python3.6/dist-packages (from yfinance) (1.0.5)\n",
            "Requirement already satisfied: numpy>=1.15 in /usr/local/lib/python3.6/dist-packages (from yfinance) (1.18.5)\n",
            "Requirement already satisfied: requests>=2.20 in /usr/local/lib/python3.6/dist-packages (from yfinance) (2.23.0)\n",
            "Requirement already satisfied: multitasking>=0.0.7 in /usr/local/lib/python3.6/dist-packages (from yfinance) (0.0.9)\n",
            "Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.6/dist-packages (from pandas>=0.24->yfinance) (2018.9)\n",
            "Requirement already satisfied: python-dateutil>=2.6.1 in /usr/local/lib/python3.6/dist-packages (from pandas>=0.24->yfinance) (2.8.1)\n",
            "Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.6/dist-packages (from requests>=2.20->yfinance) (2.9)\n",
            "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.6/dist-packages (from requests>=2.20->yfinance) (1.24.3)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/dist-packages (from requests>=2.20->yfinance) (2020.6.20)\n",
            "Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.6/dist-packages (from requests>=2.20->yfinance) (3.0.4)\n",
            "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.6/dist-packages (from python-dateutil>=2.6.1->pandas>=0.24->yfinance) (1.12.0)\n",
            "Building wheels for collected packages: yfinance\n",
            "  Building wheel for yfinance (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for yfinance: filename=yfinance-0.1.54-py2.py3-none-any.whl size=22409 sha256=33deed766c4d6639a02aff5515f2005745647a03f900626a3ac08099bd1dfd2c\n",
            "  Stored in directory: /root/.cache/pip/wheels/f9/e3/5b/ec24dd2984b12d61e0abf26289746c2436a0e7844f26f2515c\n",
            "Successfully built yfinance\n",
            "Installing collected packages: yfinance\n",
            "Successfully installed yfinance-0.1.54\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "SIOrQE0TCKFW",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "%matplotlib inline\n",
        "import numpy as np\n",
        "import pandas as pd\n",
        "from datetime import datetime, timedelta\n",
        "import statsmodels.api as sm\n",
        "import matplotlib.pyplot as plt\n",
        "import pandas_datareader as pdr\n",
        "import yfinance as yf"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "OB6ZqxqerH3-",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 235
        },
        "outputId": "ae0dbade-ac9a-4ca3-a2fc-b9c9c8f2ed6e"
      },
      "source": [
        "# pdr.famafrench.get_available_datasets()\n",
        "df_ff = pdr.data.DataReader('F-F_Research_Data_Factors', 'famafrench')[0]\n",
        "df_ff.head()"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Mkt-RF</th>\n",
              "      <th>SMB</th>\n",
              "      <th>HML</th>\n",
              "      <th>RF</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>Date</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>2015-07</th>\n",
              "      <td>1.54</td>\n",
              "      <td>-4.15</td>\n",
              "      <td>-4.12</td>\n",
              "      <td>0.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2015-08</th>\n",
              "      <td>-6.04</td>\n",
              "      <td>0.49</td>\n",
              "      <td>2.66</td>\n",
              "      <td>0.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2015-09</th>\n",
              "      <td>-3.08</td>\n",
              "      <td>-2.64</td>\n",
              "      <td>0.53</td>\n",
              "      <td>0.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2015-10</th>\n",
              "      <td>7.75</td>\n",
              "      <td>-1.97</td>\n",
              "      <td>-0.07</td>\n",
              "      <td>0.0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2015-11</th>\n",
              "      <td>0.56</td>\n",
              "      <td>3.64</td>\n",
              "      <td>-0.51</td>\n",
              "      <td>0.0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "         Mkt-RF   SMB   HML   RF\n",
              "Date                            \n",
              "2015-07    1.54 -4.15 -4.12  0.0\n",
              "2015-08   -6.04  0.49  2.66  0.0\n",
              "2015-09   -3.08 -2.64  0.53  0.0\n",
              "2015-10    7.75 -1.97 -0.07  0.0\n",
              "2015-11    0.56  3.64 -0.51  0.0"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 4
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "f-a_lphzrH4C",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 391
        },
        "outputId": "51221f62-10a4-4726-ae67-4e68f5f0b43b"
      },
      "source": [
        "start_date = datetime(2015, 1, 1)\n",
        "end_date = datetime.today()\n",
        "sectors = ['XLB', 'XLC', 'XLF', 'XLI', 'XLK', 'XLP', 'XLRE', 'XLU', 'XLV', 'XLY', 'XLE']\n",
        "\n",
        "df_sectors = pd.DataFrame()\n",
        "for sym in sectors:\n",
        "    print(sym)\n",
        "    # df = downloadpdr.DataReader(name=sym, data_source='yahoo', start=start_date, end=end_date)\n",
        "    df = yf.download(sym, start=start_date, end=end_date)\n",
        "    df = df[['Adj Close']]\n",
        "    df.columns = [sym]\n",
        "    df_sectors = pd.concat([df_sectors, df], axis=1, join='outer')"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "XLB\n",
            "[*********************100%***********************]  1 of 1 completed\n",
            "XLC\n",
            "[*********************100%***********************]  1 of 1 completed\n",
            "XLF\n",
            "[*********************100%***********************]  1 of 1 completed\n",
            "XLI\n",
            "[*********************100%***********************]  1 of 1 completed\n",
            "XLK\n",
            "[*********************100%***********************]  1 of 1 completed\n",
            "XLP\n",
            "[*********************100%***********************]  1 of 1 completed\n",
            "XLRE\n",
            "[*********************100%***********************]  1 of 1 completed\n",
            "XLU\n",
            "[*********************100%***********************]  1 of 1 completed\n",
            "XLV\n",
            "[*********************100%***********************]  1 of 1 completed\n",
            "XLY\n",
            "[*********************100%***********************]  1 of 1 completed\n",
            "XLE\n",
            "[*********************100%***********************]  1 of 1 completed\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "Y6SfFKM4rH4F",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 235
        },
        "outputId": "426586d3-23ef-47c2-e14c-16ca506f5a6e"
      },
      "source": [
        "df_sec_ret = df_sectors.resample('M').agg(lambda x: x[-1])\n",
        "df_sec_ret.index = df_sec_ret.index.to_period()\n",
        "df_sec_ret = df_sec_ret.pct_change()\n",
        "df_sec_ret.head()"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>XLB</th>\n",
              "      <th>XLC</th>\n",
              "      <th>XLF</th>\n",
              "      <th>XLI</th>\n",
              "      <th>XLK</th>\n",
              "      <th>XLP</th>\n",
              "      <th>XLRE</th>\n",
              "      <th>XLU</th>\n",
              "      <th>XLV</th>\n",
              "      <th>XLY</th>\n",
              "      <th>XLE</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>Date</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>2015-01</th>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "      <td>NaN</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2015-02</th>\n",
              "      <td>0.079681</td>\n",
              "      <td>NaN</td>\n",
              "      <td>0.058236</td>\n",
              "      <td>0.053509</td>\n",
              "      <td>0.079950</td>\n",
              "      <td>0.041441</td>\n",
              "      <td>NaN</td>\n",
              "      <td>-0.063948</td>\n",
              "      <td>0.042876</td>\n",
              "      <td>0.085441</td>\n",
              "      <td>0.045930</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2015-03</th>\n",
              "      <td>-0.049048</td>\n",
              "      <td>NaN</td>\n",
              "      <td>-0.006160</td>\n",
              "      <td>-0.025461</td>\n",
              "      <td>-0.034356</td>\n",
              "      <td>-0.019427</td>\n",
              "      <td>NaN</td>\n",
              "      <td>-0.009953</td>\n",
              "      <td>0.006406</td>\n",
              "      <td>-0.004830</td>\n",
              "      <td>-0.011552</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2015-04</th>\n",
              "      <td>0.033620</td>\n",
              "      <td>NaN</td>\n",
              "      <td>0.000830</td>\n",
              "      <td>-0.002510</td>\n",
              "      <td>0.027510</td>\n",
              "      <td>-0.007591</td>\n",
              "      <td>NaN</td>\n",
              "      <td>-0.004726</td>\n",
              "      <td>-0.010897</td>\n",
              "      <td>-0.000531</td>\n",
              "      <td>0.065739</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2015-05</th>\n",
              "      <td>0.003768</td>\n",
              "      <td>NaN</td>\n",
              "      <td>0.019478</td>\n",
              "      <td>0.003236</td>\n",
              "      <td>0.018553</td>\n",
              "      <td>0.008683</td>\n",
              "      <td>NaN</td>\n",
              "      <td>0.006332</td>\n",
              "      <td>0.045043</td>\n",
              "      <td>0.013146</td>\n",
              "      <td>-0.051887</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "              XLB  XLC       XLF  ...       XLV       XLY       XLE\n",
              "Date                              ...                              \n",
              "2015-01       NaN  NaN       NaN  ...       NaN       NaN       NaN\n",
              "2015-02  0.079681  NaN  0.058236  ...  0.042876  0.085441  0.045930\n",
              "2015-03 -0.049048  NaN -0.006160  ...  0.006406 -0.004830 -0.011552\n",
              "2015-04  0.033620  NaN  0.000830  ... -0.010897 -0.000531  0.065739\n",
              "2015-05  0.003768  NaN  0.019478  ...  0.045043  0.013146 -0.051887\n",
              "\n",
              "[5 rows x 11 columns]"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 6
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "9NScu4jCrH4H",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "9bf5db8f-a079-430a-beb6-5a21c34348ca"
      },
      "source": [
        "df_sec_ret = df_sec_ret.apply(lambda x: x-df_ff['RF']/100.0)\n",
        "df_sec_ret.dropna(axis=0, inplace=True)\n",
        "df_Y = df_sec_ret\n",
        "\n",
        "df_X = df_ff[['Mkt-RF', 'SMB', 'HML']]/100.0\n",
        "df_X = df_X.loc[df_Y.index]\n",
        "print(f'{df_Y.shape[1]} stocks, {df_X.shape[1]} factors, {df_Y.shape[0]} time steps')\n",
        "\n",
        "df_X = sm.add_constant(df_X, prepend=False)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "11 stocks, 3 factors, 23 time steps\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "xz_OxQe2rH4K",
        "colab": {}
      },
      "source": [
        "# fama_macbeth step one: time-series regression ==> factor exposures\n",
        "beta = pd.DataFrame()             # factor exposures\n",
        "for sym in df_Y.columns:\n",
        "    model = sm.OLS(df_Y[sym], df_X)\n",
        "    results = model.fit()\n",
        "    beta = pd.concat([beta, pd.DataFrame([results.params[:3]], index=[sym])])"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab_type": "code",
        "id": "ezyTC_jSrH4M",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 390
        },
        "outputId": "3cb4fad2-aed7-4b2b-9585-63239b20e6b8"
      },
      "source": [
        "beta"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Mkt-RF</th>\n",
              "      <th>SMB</th>\n",
              "      <th>HML</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>XLB</th>\n",
              "      <td>0.960951</td>\n",
              "      <td>0.173734</td>\n",
              "      <td>0.140637</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>XLC</th>\n",
              "      <td>0.888132</td>\n",
              "      <td>0.143250</td>\n",
              "      <td>-0.024562</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>XLF</th>\n",
              "      <td>1.013096</td>\n",
              "      <td>-0.146569</td>\n",
              "      <td>0.590527</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>XLI</th>\n",
              "      <td>1.073558</td>\n",
              "      <td>0.032304</td>\n",
              "      <td>0.181941</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>XLK</th>\n",
              "      <td>1.094444</td>\n",
              "      <td>-0.237579</td>\n",
              "      <td>-0.308043</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>XLP</th>\n",
              "      <td>0.757556</td>\n",
              "      <td>-0.922759</td>\n",
              "      <td>0.061853</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>XLRE</th>\n",
              "      <td>0.640816</td>\n",
              "      <td>-0.046201</td>\n",
              "      <td>0.183027</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>XLU</th>\n",
              "      <td>0.587121</td>\n",
              "      <td>-0.715389</td>\n",
              "      <td>0.174943</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>XLV</th>\n",
              "      <td>0.760626</td>\n",
              "      <td>-0.016817</td>\n",
              "      <td>-0.236447</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>XLY</th>\n",
              "      <td>1.147642</td>\n",
              "      <td>-0.035691</td>\n",
              "      <td>-0.084228</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>XLE</th>\n",
              "      <td>1.523626</td>\n",
              "      <td>0.360261</td>\n",
              "      <td>0.714848</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "        Mkt-RF       SMB       HML\n",
              "XLB   0.960951  0.173734  0.140637\n",
              "XLC   0.888132  0.143250 -0.024562\n",
              "XLF   1.013096 -0.146569  0.590527\n",
              "XLI   1.073558  0.032304  0.181941\n",
              "XLK   1.094444 -0.237579 -0.308043\n",
              "XLP   0.757556 -0.922759  0.061853\n",
              "XLRE  0.640816 -0.046201  0.183027\n",
              "XLU   0.587121 -0.715389  0.174943\n",
              "XLV   0.760626 -0.016817 -0.236447\n",
              "XLY   1.147642 -0.035691 -0.084228\n",
              "XLE   1.523626  0.360261  0.714848"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 9
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "VBCGPL1aCKFu",
        "colab_type": "text"
      },
      "source": [
        "It makes sense that utilities has lowest market premium beta; due to oil price historical collapse, it is not surprising to see a high beta there. XLE also has a big positive exposure to HML factor, reflects its capital intensive high book value."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "wgL_cShGCXXB",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}